package dk.ucn.datamatiker.jmaweather;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LocalWeatherFragment extends Fragment
{

	private View _view;
	private LocationManager _locationManager;
	private LocationListener _locationListener;
	private boolean _updating = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{

		_view = inflater.inflate(R.layout.fragment_local_weather, container, false);

		Button btnRefreshLocalWeather = (Button) _view.findViewById(R.id.btnRefreshLocalWeather);

		btnRefreshLocalWeather.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				updateWeather();
			}

		});

		updateWeather();

		return _view;

	}

	private void showWeather(Location location)
	{

		try
		{

			Geocoder geoCoder = new Geocoder(getActivity().getApplicationContext());
			List<Address> addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

			String city = addresses.get(0).getLocality();
			//final WeatherData weatherData = WeatherData.fromCity("Bronderslev");
			final WeatherData weatherData = WeatherData.fromCity(city);

			getActivity().runOnUiThread(new Runnable()
			{

				@Override
				public void run()
				{

					Toast.makeText(getActivity().getApplicationContext(), getString(R.string.updated_text), Toast.LENGTH_SHORT).show();
					
					if (weatherData != null)
					{

						TextView txtLocalWeatherLocation = (TextView) _view.findViewById(R.id.txtLocalWeatherLocation);
						txtLocalWeatherLocation.setText(weatherData.getLocation().getName());

						TextView txtGlobalWeatherDescription = (TextView) _view.findViewById(R.id.txtLocalWeatherDescription);
						txtGlobalWeatherDescription.setText(weatherData.getDescription());

						TextView txtLocalWeatherTemperature = (TextView) _view.findViewById(R.id.txtLocalWeatherTemperature);
						txtLocalWeatherTemperature.setText(Double.toString(weatherData.getTemperature()));

						TextView txtLocalWeatherTemperatureUnit = (TextView) _view.findViewById(R.id.txtLocalWeatherTemperatureUnit);
						txtLocalWeatherTemperatureUnit.setText(weatherData.getTemperatureUnit());

						ImageView imgLocalWeather = (ImageView) _view.findViewById(R.id.imgLocalWeather);
						imgLocalWeather.setImageBitmap(weatherData.getIcon());

					}

				}

			});

		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void updateWeather()
	{

		if (_locationManager == null)
		{
			_locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
		}

		if (_locationListener == null)
		{

			_locationListener = new LocationListener()
			{

				@Override
				public void onStatusChanged(String provider, int status, Bundle extras)
				{
				}

				@Override
				public void onProviderEnabled(String provider)
				{
				}

				@Override
				public void onProviderDisabled(String provider)
				{
				}

				@Override
				public void onLocationChanged(Location location)
				{
					_locationManager.removeUpdates(this);
					_updating = false;
					showWeather(location);
				}

			};

		}

		if (!_updating)
		{

			_updating = true;

			Toast.makeText(getActivity().getApplicationContext(), getString(R.string.updating_text), Toast.LENGTH_SHORT).show();

			Thread updateThread = new Thread(new Runnable()
			{

				@Override
				public void run()
				{

					Location lastKnownLocation = _locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

					if (lastKnownLocation == null)
					{
						_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 0, _locationListener);
					}
					else
					{
						_updating = false;
						showWeather(lastKnownLocation);
					}

				}
			});

			updateThread.start();

		}

	}

}
